package de.koller.aframe.plain;

import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;

public class Sprite {
	public final Graphics2D graphics;
	public final int width, height;
	public final BufferedImage image;
	public final PlainRasterInt raster;

	public Sprite( int width, int height ) {
		this( new BufferedImage( width, height, BufferedImage.TYPE_INT_ARGB ) );
	}
	
	public Sprite( BufferedImage image ) {
		this.width = image.getWidth();
		this.height = image.getHeight();
		this.image = image;
		
		int[] rasterData = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
		raster = new PlainRasterInt( rasterData, width, height );
		graphics = image.createGraphics();
	}
	
	public void release() {
		graphics.dispose();
	}
	
	public void render( Graphics2D g, float x, float y, float w, float h, float r ) {
		AffineTransform a = createTransform( x, y, w, h, r );
		g.drawImage( image, a, null );
	}
	
	protected AffineTransform createTransform( float x, float y, float w, float h, float r ) {
		AffineTransform a = new AffineTransform();
		a.translate( x, y );
		a.rotate( r, w*.5f, h*.5f );
		a.scale( w / width, h / height );
		return a;
	}
	
}
